package com.dream.wuliu.view;

import java.util.List;

import org.androidannotations.annotations.AfterTextChange;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.WindowFeature;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

import com.actionbarsherlock.view.Window;
import com.beardedhen.androidbootstrap.BootstrapEditText;
import com.dream.vo.api.ApiOrder;
import com.dream.vo.api.ApiResult;
import com.dream.wuliu.service.OrderService;
import com.dream.wuliu.service.ServiceFactory;
import com.dream.wuliu.utils.GsonHelper;
import com.dream.wuliu.utils.MyUtils;
import com.dream.wuliu.view.adapter.OrderListAdapter;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnPullEventListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.State;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

/**
 * 
 * @author EvanYu
 *
 */
@EActivity(R.layout.portal_main)
@WindowFeature({(int)Window.FEATURE_INDETERMINATE_PROGRESS })
public class PortalActivity extends BaseActivity {
	
	@ViewById(android.R.id.tabhost)
	TabHost tabHost;
	
	@ViewById(R.id.plMyTask)
	PullToRefreshListView plMyTask;
	
	@ViewById(R.id.etCustomer)
	BootstrapEditText etCustomer;
	
	@ViewById(R.id.etPhone)
	BootstrapEditText etPhone;
	
	private OrderListAdapter adapter = null;

	private int[] mTitles = new int[] { R.string.txt_portal_index, R.string.txt_portal_menu};
	
	@AfterViews
	public void init(){
		
		initTabs();
		doPullRefresh();
		getListData();
	}

	private void initTabs() {
		
		tabHost.setup();
		
		tabHost.addTab(buildTabSpec(R.id.portalIndex,mTitles[0]));
		tabHost.addTab(buildTabSpec(R.id.portalMeun,mTitles[1]));

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {

		super.onSaveInstanceState(outState);
	}

	private TabSpec buildTabSpec(int resourceId,int titleId) {
		String title = getString(titleId);
		TabSpec newTabSpec = tabHost.newTabSpec(title);
		newTabSpec.setContent(resourceId);
		newTabSpec.setIndicator(title);
		return newTabSpec;
	}
	
	/******index*******/
	/*@ViewById(R.id.etBarcode)
	BootstrapEditText etBarcode;
	
	@ViewById(R.id.btnScanBarcode)
	BootstrapButton btnScanBarcode;
	
	@Click(R.id.btnScanBarcode)
	void sacanBarcodeClick(){
		doZXing();
	}
	
	private static final int REQUEST_CODE = 200;
	private void doZXing() {
		
		
        Intent intent = new Intent();
        intent.setAction(Intents.Scan.ACTION);
        intent.putExtra(Intents.Scan.CHARACTER_SET, AppConstants.ENCODE_UTF_8);
        intent.putExtra(Intents.Scan.WIDTH, 600);
        intent.putExtra(Intents.Scan.HEIGHT, 400);
        intent.setClass(this, CaptureActivity.class);
        startActivityForResult(intent, REQUEST_CODE);
        
    }
	
	 @OnActivityResult(REQUEST_CODE)
	 void onResult(int resultCode, Intent intent) {
	        if (intent != null && resultCode == Activity.RESULT_OK) {
	        	etBarcode.setText(intent.getStringExtra(Intents.Scan.RESULT));
	        }
	 }
	 */
	 /******index*******/
	
	private void doPullRefresh() {
		
		plMyTask.setOnPullEventListener(new OnPullEventListener<ListView>() {
			@Override
			public void onPullEvent(PullToRefreshBase<ListView> refreshView, State state, Mode direction) {
				if (state.equals(State.PULL_TO_REFRESH)) {
					showProgress();
					getListData();
				}
			}
		});
		plMyTask.setOnItemClickListener(new OrderItemClick());
		
	}
	
	@Background
	public void getListData(){
		
		final String customerName = etCustomer.getText().toString().trim();
		final String mobile = etPhone.getText().toString().trim();
		String gson = new ServiceFactory.OrderServiceHelper() {
			@Override
			public ApiResult doService(OrderService service) {
				return service.list(customerName, mobile);
			}
		}.execute(this);
		setListData(GsonHelper.orderList(gson));
	}
	
	@UiThread
	public void setListData( List<ApiOrder> li){
		hideProgress();
		adapter=new OrderListAdapter(this,li);
		plMyTask.setAdapter(adapter);
		
	}
	
	@AfterTextChange(R.id.etCustomer)
	 void afterTextChangedOnEtCustomer() {
		String str = etCustomer.getText().toString();
		if(MyUtils.isNotBlank(str)){
			doFilter(str);
		}
	 }
	
	@AfterTextChange(R.id.etPhone)
	 void afterTextChangedOnEtPhone() {
		String str = etPhone.getText().toString();
		if(MyUtils.isNotBlank(str) && str.length() > 5){
			doFilter(str);
		}
	 }
	
	private void doFilter( String str) {
		
		if (adapter != null && adapter.getFilter() != null) {
			adapter.getFilter().filter(str);
		}
		
	}
	
	
	private  class OrderItemClick implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			ApiOrder vo = (ApiOrder)parent.getItemAtPosition(position);
			OrderDetailActivity_.intent(PortalActivity.this).id(vo.getId()).start();
		}
	}

}
